package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.TrainClassroom;
import com.sihai.ehr.model.biz.TrainOrganizationCredit;
import com.sihai.ehr.service.TrainCreditService;

/**
 * 培训组织学分服务实现类
 * 
 * @author henry
 * 
 */
@Service("trainCreditService")
public class TrainCreditServiceImpl implements TrainCreditService {
	@Resource
	DataAccessor dataAccessor;

	@Override
	public TrainOrganizationCredit getTrainOrganizationCredit(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(TrainOrganizationCredit.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取培训学分设置明细数据库发生异常】【异常说明：+" + e.getMessage() + "】【发生地点：TrainCreditServiceImpl.getTrainOrganizationCredit;参数id=" + id + " 】");
		}
	}

	@Override
	public LimitedList<TrainOrganizationCredit> getTrainOrganizationCredits(final Long organizationId, Long dutyId,final int year, Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(TrainOrganizationCredit.class, new QueryCreator() {

				@Override
				public Query createQuery(Session session) {
					DynamicQuery dynamicQuery = new DynamicQuery("from TrainOrganizationCredit t where 1 <> 2");
					dynamicQuery
					.append(" and t.organization.treePath like '%,"
							+ organizationId + ",%'");
					if(year > 0){
						dynamicQuery.append(" and t.year=" + year);
					}
					return dynamicQuery.build(session);
				}
			}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取培训学分设置时数据库发生异常】【异常说明：+" + e.getMessage() + "】【发生地点：TrainCreditServiceImpl.getTrainOrganizationCredits;参数organizationId="
					+ organizationId + "】");
		}
	}

	@Override
	public void save(TrainOrganizationCredit trainOrganizationScore) {
		if (trainOrganizationScore == null || trainOrganizationScore.getOrganization() == null) {
			throw new RuntimeException("【trainOrganizationScore对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(trainOrganizationScore);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培培训学分设置对象到数据库发生异常】【异常说明：" + e.getMessage() + "】【发生地点：TrainCreditServiceImpl.save】");
		}
	}

	@Override
	public void deleteTrainOrganizationCredit(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat.format("delete from TrainOrganizationCredit t where t.id in ({0})", ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移出培训学分设置信息数据库发生异常】【异常说明：" + e.getMessage() + "】【发生地点：TrainCreditServiceImpl.deleteTrainOrganizationCredit】");
		}
	}

}
